package com.amon.amonplayer.live;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.amon.amonplayer.R;
import com.amon.amonplayer.camera.widget.CameraFocusView;
import com.amon.amonplayer.camera.widget.CameraView;

public class LivePushActivity extends AppCompatActivity implements
        CameraView.FocusListener {
    private final String mPushUrl = "rtmp://ws.91go.vip/myapp/mystream";
    private CameraView mCameraView;
    private CameraFocusView mFocusView;
    private BaseMediaPush mMediaPush;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.live_push);

        mCameraView = findViewById(R.id.camera_view);
        mCameraView.setOnFocusListener(this);
        mFocusView = findViewById(R.id.camera_focus_view);

        findViewById(R.id.live_button).setOnClickListener(v -> {
            mMediaPush = new DefaultMediaPush(this, mCameraView.getEglContext(), mCameraView.getTextureId());
            mMediaPush.initVideo(mPushUrl, 720 / 2, 1280 / 2);
            mMediaPush.startPush();
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mMediaPush != null) {
            mMediaPush.stopPush();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mCameraView != null) {
            mCameraView.onDestroy();
        }
    }

    @Override
    public void beginFocus(int x, int y) {
        mFocusView.beginFocus(x, y);
    }

    @Override
    public void endFocus(boolean success) {
        mFocusView.endFocus(success);
    }
}
